<script setup>
import dayjs from 'dayjs';
import { saveAs } from 'file-saver';
import { ElMessage } from 'element-plus';
import axios from 'axios';
// 是否正在生成
const loading = ref(false);

// 生成PDF
const handleMakePDF = async () => {
  loading.value = true;
  const res = await axios.get('/api/report');
  // 如果响应正常， 保存
  if(res.data.code == 200 ) {
    saveAs(new Blob([new Int8Array(res.data.blob.data)]), `${dayjs().format('YYYY_MM_DD_HH_mm_ss')}.pdf`);
    ElMessage.success('导出成功')
  }
  // 如果响应异常，则提示
  else {
    ElMessage(res.data.message);
  }
  loading.value = false;
}
</script>

<template>
  <div style="text-align: center; padding-top: 100px;">
    <el-button type="primary" @click="handleMakePDF"> 生成PDF</el-button>
    <div style="margin-top: 100px;" >
      <div v-loading="loading" element-loading-text="正在导出中...">

      </div>
    </div>
  </div>
</template>

<style scoped>
</style>
